package com.glodon.paas.document.web.controller;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.glodon.paas.consts.StringConst;
import com.glodon.paas.document.api.bean.ProjectResource;
import com.glodon.paas.document.service.ProjectService;
import com.glodon.paas.document.web.util.SecurityUtils;

/**
 * 项目权限操作
 * @author wangchen
 */
@Controller
@RequestMapping("/privilege")
public class ProjectPrivilegeController {

    public static final Logger LOGGER = LoggerFactory.getLogger(ProjectPrivilegeController.class);
    @Autowired
    private ProjectService     projectService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public void getPrivilege(@RequestParam("fileId") String fileId, 
                            @RequestParam(value="roleId", required=false ) String roleId,
                            ModelMap model) {
        model.addAttribute(StringConst.ITEMS, projectService.getFileRolePrivilege(SecurityUtils.getCurrentUser().getId(), fileId, roleId));
    }
    
    @RequestMapping(value = "", method = RequestMethod.POST)
    public void createPrivileges(@RequestBody ProjectResource resource, ModelMap model) {
        this.updatePrivileges(resource, model);
    }
    
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public void updatePrivileges(@RequestBody ProjectResource resource, ModelMap model) {
        String id = projectService.savePrivilege(SecurityUtils.getCurrentUser().getId(), resource.getFileId(), resource.getRole().getId(), resource.getPrivileges());
        if(StringUtils.isNotBlank(id)){
            model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
        } else {
            model.addAttribute(StringConst.RESULT, StringConst.FAIL);
        }
    }
}
